//
//  GGScrollViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/3/30.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGScrollViewController.h"
#import "GGCarScrollView.h"
//machPort代理
@interface GGScrollViewController ()<NSMachPortDelegate>

@property (nonatomic, strong) NSMutableArray *notifications;
@property (nonatomic, strong) NSThread *notificationThread;
@property (nonatomic, strong) NSLock *notificationLock;
@property (nonatomic, strong) NSMachPort *notificationPort;

@end

@implementation GGScrollViewController{
    NSArray <NSString *>*_imageNameList; //设置全局数据
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //t导航栏透明
//     [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//     [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
//     [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self loadData];
    [self setUpUI];
//    [self setUpNotification];
//    [self resetMachPort];
    [self notificaitonQueue];
}
#pragma mark - notificationqueue
- (void)notificaitonQueue{
    //添加观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(text) name:@"notificationQueue" object:nil];
    
    //notificationQueue
    /**
     存在多个通知，合并了相同的通知，只发送一个
     */
    for (int i = 0; i < 3; i ++) {
        NSNotification *notification = [NSNotification notificationWithName:@"notificationQueue" object:nil];
        //将相同名称的通知添加到队列中
        [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
    }
}

#pragma mark - machport
- (void)resetMachPort{
    self.notifications = [[NSMutableArray alloc]init];
    self.notificationThread = [NSThread currentThread];
    self.notificationLock = [[NSLock alloc]init];
    self.notificationPort = [[NSMachPort alloc]init];
    //指定NSMachPortDelegate代理
    self.notificationPort.delegate = self;
    
    //通过runloop添加端口源 
    [[NSRunLoop currentRunLoop] addPort:self.notificationPort forMode:NSRunLoopCommonModes];
    
    //添加观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processNotification:) name:@"machportNotification" object:nil];
    
    //异步发送通知
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"machportNotification" object:nil];
    });
    
}
#pragma mark - machportDelegate
- (void)handleMachMessage:(void *)msg{
    [self.notificationLock lock];
    while ([self.notifications count]) {
        NSNotification *notification = [self.notifications objectAtIndex:0];
        [self.notifications removeObjectAtIndex:0];
        [self.notificationLock unlock];
        [self processNotification:notification];
        [self.notificationLock lock];
    }
    [self.notificationLock unlock];
}

//
- (void)processNotification:(NSNotification *)notification{
    
    if([NSThread currentThread] != self.notificationThread){
        [self.notificationLock lock];
        [self.notifications addObject:notification];
        [self.notificationLock unlock];
        //向期望端口发送消息
        [self.notificationPort sendBeforeDate:[NSDate date] components:nil from:nil reserved:0];
    }else{
        NSLog(@"current thread = %@",[NSThread currentThread]);
        NSLog(@"process notification");
    }
    
}


#pragma mark - 界面布局
- (void)setUpUI{
    //NSNotificationCenter
    /**
     添加观察者
     */
    GDWeakSelf
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(text) name:@"nihao" object:nil];
    //queue是nil,就默认在发送通知中执行
//    [[NSNotificationCenter defaultCenter]addObserverForName:@"nihao" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
//        //注意需要解决循环引用
//        [weakSelf text];
//    }];
   /**
    重定向方法1.通过[NSOperationQueue mainQueue]回到主线程刷新UI
    */
    [[NSNotificationCenter defaultCenter]addObserverForName:@"nihao" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        //回到主线程刷新UI
        //注意需要解决循环引用
        [weakSelf text];
    }];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIImage *image = [UIImage imageNamed:@"zoo"];
    self.view.layer.contents = (id)image.CGImage;
    
//    self.title = @"stateView高度问题";
//
//    GGCarScrollView *carScrollView = [[GGCarScrollView alloc]initWithFrame:self.view.bounds];
//    carScrollView.imageNameList = _imageNameList;
//    [self.view addSubview:carScrollView];
    
    self.title = @"NSNotification";
}
#pragma mark - NSNotification 通知
- (void)setUpNotification{
    //NSNotification
    /**
     1.手动自定义通知对象,发送通知
     */
//    NSNotification *notification = [NSNotification notificationWithName:@"nihao" object:nil];
    //或者
//    NSNotification *notification = [NSNotification notificationWithName:@"nihao" object:nil userInfo:nil];
    //或者
    /**
        
     */
//    NSNotification *notification = [[NSNotification alloc]initWithName:@"nihao" object:nil userInfo:nil];
//    [[NSNotificationCenter defaultCenter] postNotification:notification];
    /**
     2.通知中心自定义通知并发送
     */
    [[NSNotificationCenter defaultCenter] postNotificationName:@"nihao" object:nil];
    
}

- (void)text{
    NSLog(@"NSNotification ---- current thread %@",[NSThread currentThread]);
}

#pragma mark - 加载数据
- (void)loadData{
    //对于数组的使用
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < 4 ; i ++) {
        NSString *nameStr = [NSString stringWithFormat:@"common_h%d",i];
        [array addObject:nameStr];
    }
    _imageNameList = array.copy;
    
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
